/**
 * 
 */
package com.asset.salesforce.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.asset.salesforce.domain.Account;
import com.asset.salesforce.domain.Contact;
import com.asset.salesforce.domain.Employee;
import com.asset.salesforce.domain.FollowCenter;
import com.asset.salesforce.domain.Lead;

// TODO: Auto-generated Javadoc
/**
 * The Class FollowCenterDaoImpl.
 *
 * @author Hisham
 */
@Repository
public class FollowCenterDaoImpl extends BasicDaoImpl<FollowCenter>{

	/** The session factory. */
	@Autowired
	private SessionFactory sessionFactory;
	
	/**
	 * Addfollow center.
	 *
	 * @param followCenter the follow center
	 */
	public void addfollowCenter(FollowCenter followCenter) {
		try{
			sessionFactory.getCurrentSession().save(followCenter);
			}catch(Exception ex){
				ex.printStackTrace();
			}
	}
	
	/**
	 * Check if current follow that lead.
	 *
	 * @param employee the employee
	 * @param lead the lead
	 * @return the list
	 */
	@SuppressWarnings("unchecked")
	public List<FollowCenter> checkIfCurrentFollowThatLead(Employee employee,Lead lead){
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from FollowCenter where follower.id = :empId and followedType= :followedId and followedId = :leadId");
		query.setParameter("empId", employee.getId());
		query.setParameter("followedId", "lead");
		query.setParameter("leadId", lead.getId());
		List<FollowCenter> centers  = new ArrayList<FollowCenter>();
		centers =  query.list();
		return centers;		
	}
	
	/**
	 * Check if current follow that account.
	 *
	 * @param employee the employee
	 * @param account the account
	 * @return the list
	 */
	@SuppressWarnings("unchecked")
	public List<FollowCenter> checkIfCurrentFollowThatAccount(Employee employee,Account account){
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from FollowCenter where follower.id = :empId and followedType= :followedId and followedId = :accountId");
		query.setParameter("empId", employee.getId());
		query.setParameter("followedId", "account");
		query.setParameter("accountId", account.getId());
		List<FollowCenter> centers  = new ArrayList<FollowCenter>();
		centers =  query.list();
		return centers;		
	}
	
	/**
	 * Check if current follow that contact.
	 *
	 * @param employee the employee
	 * @param contact the contact
	 * @return the list
	 */
	@SuppressWarnings("unchecked")
	public List<FollowCenter> checkIfCurrentFollowThatContact(Employee employee,Contact contact){
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from FollowCenter where follower.id = :empId and followedType= :followedId and followedId = :accountId");
		query.setParameter("empId", employee.getId());
		query.setParameter("followedId", "contact");
		query.setParameter("accountId", contact.getId());
		List<FollowCenter> centers  = new ArrayList<FollowCenter>();
		centers =  query.list();
		return centers;		
	}
	
	/**
	 * Check if current follow that people.
	 *
	 * @param follower the follower
	 * @param followed the followed
	 * @return the list
	 */
	@SuppressWarnings("unchecked")
	public List<FollowCenter> checkIfCurrentFollowThatPeople(Employee follower,Employee followed){
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from FollowCenter where follower.id = :empId and followedType= :followedId and followedId = :accountId");
		query.setParameter("empId", follower.getId());
		query.setParameter("followedId", "people");
		query.setParameter("accountId", followed.getId());
		List<FollowCenter> centers  = new ArrayList<FollowCenter>();
		centers =  query.list();
		return centers;		
	}
}
